package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment temp = this.get(appointment.getId());
        if(!Appointment.STATUS_APPOINTMENT.equals(temp.getStatus())){
            throw new BusinessException("预约单只有预约中状态时才允许修改");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Appointment appointment = appointmentMapper.selectByPrimaryKey(dictId);
            appointmentMapper.updateByData(appointment);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancel(Long id) {
        Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("预约单只有预约中状态时才允许取消");
        }
        appointmentMapper.changeStatus(id, Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id) {
        Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("预约单只有预约中状态时才允许设置到店操作");
        }
        appointmentMapper.updateArrical(id, Appointment.STATUS_ARRIVAL, new Date());
    }

    @Override
    public void renewal() {
        Date date = new Date();
        appointmentMapper.renewal(date,Appointment.STATUS_APPOINTMENT,Appointment.STATUS_OVERTIME);
    }

    @Override
    public void changeStatus(Long appointmentId, Integer statusSettle) {
        appointmentMapper.changeStatus(appointmentId, statusSettle);
    }

    @Override
    public void recovery(Long id) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (appointment == null || Appointment.DATA_RETAIN.equals(appointment.getData())) {
            throw new BusinessException("参数异常");
        }
        appointmentMapper.dataRecovery(id, Appointment.DATA_RETAIN);
    }

    @Scheduled(cron = "0 0 0/1 * * *")
    public void timer() {
        Date date = new Date();
        for (Appointment appointment : appointmentMapper.selectAll()){
            if(appointment.getAppointmentTime().getTime() <= date.getTime() && appointment.getStatus().equals(0) && appointment.getData().equals(0)){
                appointmentMapper.timerStatus(appointment.getId(), Appointment.STATUS_OVERTIME);
            }
        }
    }
}
